package com.czh.springboot04.MyController;

import com.czh.springboot04.entity.MyNotice;
import com.czh.springboot04.service.MyNoticeService;
import com.czh.springboot04.util.R;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("mynotice")
public class MyNoticeController {
    @Autowired
    private MyNoticeService myNoticeService;
    @PostMapping("/mynoticeAdd")
    public R mynoticeAdd(@RequestBody MyNotice myNotice){
        int i = myNoticeService.mynoticeAdd(myNotice);
        if (i>0){
            return R.ok(1);
        }else{
            return R.fail("添加失败");
        }
    }
    @PutMapping("/mynoticeUpdate")
    public R mynoticeUpdate(@RequestBody MyNotice myNotice){
        int i = myNoticeService.mynoticeUpdate(myNotice);
        System.out.println(i);
        if (i>0){
            return R.ok(1);
        }else{
            return R.fail("修改失败");
        }
    }
    @DeleteMapping("/mynoticeDelete/{id}")
    public R mynoticeDelete(@PathVariable int id){
        int i = myNoticeService.mynoticeDelete(id);
        if (i>0){
            return R.ok(1);
        }else{
            return R.fail("删除失败");
        }
    }
    @GetMapping("/mynoticeSelectAll")
    public R mynoticeSelectAll(@RequestParam(required = false, defaultValue =  "1") int pageNum,
                               @RequestParam(required = false, defaultValue =  "1") int pageSize){
        PageInfo<MyNotice> myNoticePageInfo = myNoticeService.mynoticeSelectAll(pageNum, pageSize);
        return R.ok(myNoticePageInfo);
    }
    @GetMapping("/mynoticeSelectByMore")
    public R mynoticeSelectByMore(@RequestParam(required = false, defaultValue =  "1") int pageNum,
                                  @RequestParam(required = false, defaultValue =  "3") int pageSize){
        PageInfo<MyNotice> myNoticePageInfo = myNoticeService.mynoticeSelectByMore(pageNum, pageSize);
        return R.ok(myNoticePageInfo);
    }
    @GetMapping("/mynoticeSelectByName")
    public R mynoticeSelectByName(@RequestParam(required = false, defaultValue =  "1") int pageNum,
                                  @RequestParam(required = false, defaultValue =  "3") int pageSize,
                                 @RequestParam(required = false, defaultValue =  "") String name){
        PageInfo<MyNotice> myNoticePageInfo = myNoticeService.mynoticeSelectByName(pageNum, pageSize, name);
        return R.ok(myNoticePageInfo);
    }
}
